<template>
  <div class="com-container">
    <div class="title" :style="titleStyle">
      <span>{{ '▎ ' + selectTitle }}</span>
      <span class="select_ico iconfont title-icon" @click="selectedShow">&#xe6eb;</span>
      <div
        class="select-con"
        v-for="item in selected"
        :key="item.key"
        v-show="selectShow"
        :style="
      marginStyle"
      >
        <div class="select-item" @click="handleSelect(item.key)">{{item.text}}</div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "",

  data() {
    return {
      echarts: null,
      echartsInit: null,
      allData: null,
      selectData: "map",
      selectType: null,
      selectShow: false,
      titleFontSize: 0
    };
  },

  computed: {
    ...mapState(['theme']),
    marginStyle() {
      return { marginLeft: this.titleFontSize + "px" };
    },

    titleStyle() {
      return { fontSize: this.titleFontSize + "px" };
    },

    selected() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type.filter(item => {
          return item.key != this.selectData;
        });
      }
    },

    selectTitle() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.selectData].title;
      }
    }
  },

  mounted() {
    this.init();
    this.getData();
    window.addEventListener("resize", this.screenData);
    this.screenData();
  },

  destroyed() {
    window.removeEventListener("resize", this.screenData);
  },

  methods: {
    handleSelect(current) {
      this.selectData = current;
      this.upData();
      this.selectShow = !this.selectShow;
    },

    selectedShow() {
      this.selectShow = !this.selectShow;
    },

    init() {
      this.echarts = require("echarts");
      this.echartsInit = this.echarts.init(this.$refs.trend_ref,this.theme);
      const InitOption = {
        xAxis: {
          type: "category",
          boundaryGap: false //线条是否要紧贴Y轴边缘,是否需要间隔
        },
        yAxis: { type: "value" },
        tooltip: {
          trigger: "axis"
        },
        legend: {
          //控制图例的样式
          left: 20,
          top: "15%",
          icon: "circle" //设置为圆形
        },
        grid: {
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true // 距离是包含坐标轴上的文字
        }
      };
      this.echartsInit.setOption(InitOption);
    },
    getData() {
      this.axios.get("http://127.0.0.1:8888/api/trend").then(response => {
        this.allData = response.data;
        //console.log(this.allData);
        this.upData();
      });
    },
    upData() {
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)"
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)"
      ];
      const timeArr = this.allData.common.month;
      //   console.log(timeArr);
      const diquArry = this.allData[this.selectData].data;
      // console.log(diquArry)
      const seriesArry = diquArry.map((item, index) => {
        return {
          type: "line",
          data: item.data,
          stack: "map",
          name: item.name,
          areaStyle: {
            color: new this.echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1, //4个参数用于配置渐变色的起止位置, 这4个参数依次对应右/下/左/上四个方位. 而0 0 0 1则代表渐变色从正上方开始
              [
                { offset: 0, color: colorArr1[index] },
                // {offset: 0.5, color: '#888'},
                { offset: 1, color: colorArr2[index] }
              ] //数组, 用于配置颜色的渐变过程. 每一项为一个对象, 包含offset和color两个参数. offset的范围是0 ~ 1, 用于表示位置
            )
            // 纹理填充
            // color: {
            //   //image: imageDom, // 支持为 HTMLImageElement, HTMLCanvasElement，不支持路径字符串
            //   repeat: "repeat" // 是否平铺，可以是 'repeat-x', 'repeat-y', 'no-repeat'
            // }
          }
        };
      });
      const nameArr = diquArry.map(item => {
        return item.name;
      });
      //console.log(seriesArry)
      const upOption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: nameArr //图标上面的案例说明
        },
        series: seriesArry
      };
      this.echartsInit.setOption(upOption);
    },
    screenData() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      const screenOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      };
      this.echartsInit.setOption(screenOption);
      this.echartsInit.resize();
    }
  },

  watch:{
    theme(){
      this.echartsInit.dispose() // 销毁当前的图表
      this.init()
      this.screenData()
      this.upData()
    }
  },
};
</script>
<style>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
}
.title-icon {
  margin-left: 10px;
  cursor: pointer;
}
.select-con {
  background-color: #222733;
}
</style>
